﻿/*
    Copyright (C) <2007-2013> Kay Diefenthal

    SimpleRadio is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SimpleRadio is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SimpleRadio.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using Microsoft.MediaCenter.UI;

namespace SimpleRadio
{
    /// <summary>
    /// <see cref="DefaultView"/> Class.
    /// </summary>
    public class DefaultView : ModelItem
    {
        #region Private Fields

        private Choice _baseFilter;
        private Choice _subFilter;
        private IList _content;
        private ServiceOffer _serviceOffer;
        private Choice _sortFilters;

        #endregion

        #region Properties

        /// <summary>
        /// The Sort Filters
        /// </summary>
        public virtual Choice SortFilters
        {
            get { return _sortFilters; }
            set
            {
                if (_sortFilters != value)
                {
                    if (_sortFilters != null)
                        _sortFilters.ChosenChanged -= OnSortFiltersChanged;
                    _sortFilters = value;
                    if (_sortFilters != null)
                        _sortFilters.ChosenChanged += OnSortFiltersChanged;
                    OnSortFiltersChanged(_sortFilters, EventArgs.Empty);
                    FirePropertyChanged("SortFilters");
                }
            }
        }

        /// <summary>
        /// The Base Filters
        /// </summary>
        public virtual Choice BaseFilter
        {
            get { return _baseFilter; }
            set
            {
                if (_baseFilter != value)
                {
                    if (_baseFilter != null)
                        _baseFilter.ChosenChanged -= OnBaseFilterChanged;
                    _baseFilter = value;
                    if (_baseFilter != null)
                        _baseFilter.ChosenChanged += OnBaseFilterChanged;
                    OnBaseFilterChanged(_baseFilter, EventArgs.Empty);
                    FirePropertyChanged("BaseFilter");
                }
            }
        }

        /// <summary>
        /// The Sub Filters
        /// </summary>
        public virtual Choice SubFilter
        {
            get { return _subFilter; }
            set
            {
                if (_subFilter != value)
                {
                    if (_subFilter != null)
                        _subFilter.ChosenChanged -= OnSubFilterChanged;
                    _subFilter = value;
                    if (_subFilter != null)
                        _subFilter.ChosenChanged += OnSubFilterChanged;
                    OnSubFilterChanged(_subFilter, EventArgs.Empty);
                    FirePropertyChanged("SubFilter");
                }
            }
        }

        /// <summary>
        /// The Content
        /// </summary>
        public virtual IList Content
        {
            get { return _content; }
            set { if (_content != value) { _content = value; FirePropertyChanged("Content"); } }
        }

        /// <summary>
        /// The Service Offer
        /// </summary>
        public ServiceOffer ServiceOffer
        {
            get
            {
                if (_serviceOffer == null)
                    return _serviceOffer = new ServiceOffer();
                return _serviceOffer;
            }
            set
            {
                if (_serviceOffer != value)
                {
                    _serviceOffer = value; FirePropertyChanged("ServiceOffer");
                }
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// ClickedItem
        /// </summary>
        /// <param name="service">service <seealso cref="Service"/></param>
        public void ClickedItem(Service service)
        {
            if (ServiceInvoked != null)
                ServiceInvoked(service, EventArgs.Empty);
        }

        /// <summary>
        /// FocusedItem
        /// </summary>
        /// <param name="service">service <seealso cref="Service"/></param>
        public void FocusedItem(Service service)
        {
            _serviceOffer = new ServiceOffer(service.DisplayName, service.Country, service.Region, service.Genres);
            FirePropertyChanged("ServiceOffer");
        }

        #endregion

        #region Public Events

        /// <summary>
        /// SortFiltersChanged
        /// </summary>
        public event EventHandler SortFiltersChanged;

        /// <summary>
        /// BaseFilterChanged
        /// </summary>
        public event EventHandler BaseFilterChanged;

        /// <summary>
        /// SubFilterChanged
        /// </summary>
        public event EventHandler SubFilterChanged;

        /// <summary>
        /// ServiceInvoked
        /// </summary>
        public event EventHandler ServiceInvoked;

        #endregion

        #region
        /// <summary>
        /// OnSortFiltersChanged
        /// </summary>
        /// <param name="sender">sender <seealso cref="System.Object"/></param>
        /// <param name="args">args <seealso cref="System.EventArgs"/></param>
        protected virtual void OnSortFiltersChanged(object sender, EventArgs args)
        {
            if (SortFiltersChanged != null)
                SortFiltersChanged(this, EventArgs.Empty);
        }
        /// <summary>
        /// OnBaseFilterChanged
        /// </summary>
        /// <param name="sender">sender <seealso cref="System.Object"/></param>
        /// <param name="args">args <seealso cref="System.EventArgs"/></param>
        protected virtual void OnBaseFilterChanged(Object sender, EventArgs args)
        {
            if (BaseFilterChanged != null)
                BaseFilterChanged(this, EventArgs.Empty);
        }
        /// <summary>
        /// OnSubFilterChanged
        /// </summary>
        /// <param name="sender">sender <seealso cref="System.Object"/></param>
        /// <param name="args">args <seealso cref="System.EventArgs"/></param>
        protected virtual void OnSubFilterChanged(Object sender, EventArgs args)
        {
            if (SubFilterChanged != null)
                SubFilterChanged(this, EventArgs.Empty);
        }

        #endregion

    }
}
